package com.zmc.order.config.shard;

import com.google.common.collect.Range;
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 多分片键策略(order_sn)
 * @Author：zmc
 * @Date：2024/10/13 9:22
 */
public class OrderColumnStrategy implements StandardShardingAlgorithm<String> {

   public static final String SHARD_COMPLEX_STRATEGY = "order_column_strategy";

    private Properties props = new Properties();

    /**
     * collection: 当前分片键所有表list
     * preciseShardingValue： 分片键（k-v），此处是单分片键，只会出现一对
     */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        //分片键逻辑，精准查询。可根据项目需求编写
        return collection.stream().findFirst().get();
    }

    /**
     * collection: 当前分片键所有表list
     * rangeShardingValue： 分片键（k-v），
     * 注：k（分片键），v(范围，举例：0-2000;sql中between查询,且是分片键范围查询，则会触发)，其中v格式需要转义
     */
    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<String> rangeShardingValue) {
        //分片键逻辑，范围查询。可根据项目需求编写
        return null;
    }

    @Override
    public Properties getProps() {
        return props;
    }

    @Override
    public void init(Properties properties) {
        this.props = properties;
    }

    @Override
    public String getType() {
        return SHARD_COMPLEX_STRATEGY;
    }
}
